package com.qyer.search.model.lastminute;

import static com.qyer.commons.utils.CommonUtils.cast2Double;
import static com.qyer.commons.utils.CommonUtils.cast2Int;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qyer.search.enums.TagStatus;
import com.qyer.search.enums.TagType;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Map;

/**
 * User: Z J Wu Date: 2016/01/05 Time: 19:01 Package: com.qyer.search.model.lastminute
 */
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.PUBLIC_ONLY)
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonFilter("TagFilter")
public class Tag {

  private static final String FIELD_ID = "tid";
  private static final String FIELD_TAGNAME = "tag_name";
  private static final String FIELD_TAGTYPE = "tag_type";
  private static final String FIELD_COEFFICIENT = "coefficient";
  private static final String FIELD_STATUS = "status";

  @JsonIgnore
  private int id;

  @JsonProperty("tag_name")
  private String tagName;

  @JsonIgnore
  private TagType tagType;

  @JsonIgnore
  private double coefficient;

  @JsonIgnore
  private TagStatus status;

  public Tag() {
  }

  public Tag(int id, String tagName, TagStatus status) {
    this.id = id;
    this.tagName = tagName;
    this.status = status;
  }

  public static Tag buildTag(Map<String, Object> dataMap) {
    if (MapUtils.isEmpty(dataMap)) {
      return null;
    }
    int id = cast2Int(dataMap.get(FIELD_ID));
    String tagName = String.valueOf(dataMap.get(FIELD_TAGNAME));
    TagStatus tagStatus = TagStatus.parse(cast2Int(dataMap.get(FIELD_STATUS)));
    Tag tag = new Tag(id, tagName, tagStatus);
    tag.setTagType(TagType.parseType(cast2Int(dataMap.get(FIELD_TAGTYPE))));
    tag.setCoefficient(cast2Double(dataMap.get(FIELD_COEFFICIENT)));
    return tag;
  }

  public boolean forExternalize() {
    //return TagType.STANDARD.equals(this.tagType);
    return true;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getTagName() {
    return tagName;
  }

  public void setTagName(String tagName) {
    this.tagName = tagName;
  }

  public TagType getTagType() {
    return tagType;
  }

  public void setTagType(TagType tagType) {
    this.tagType = tagType;
  }

  public double getCoefficient() {
    return coefficient;
  }

  public void setCoefficient(double coefficient) {
    this.coefficient = coefficient;
  }

  public TagStatus getStatus() {
    return status;
  }

  public void setStatus(TagStatus status) {
    this.status = status;
  }

  @Override public String toString() {
    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("id", id)
                                                                      .append("tagName", tagName)
                                                                      .append("tagType", tagType)
                                                                      .append("coefficient",
                                                                              coefficient)
                                                                      .append("status", status)
                                                                      .toString();
  }

  public static void main(String[] args) throws JsonProcessingException {
    Tag t = new Tag(1, "吃喝", TagStatus.ENABLE);
    t.setTagType(TagType.STANDARD);
    t.setCoefficient(1.2d);
    System.out.println(t);

    ObjectMapper objectMapper = new ObjectMapper();
    String jsonString = objectMapper.writeValueAsString(t);
    System.out.println(jsonString);
  }
}
